package com.headcaselabs.queue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xerox.amazonws.sqs.MessageQueue;
/**
 * Holds parameters for the configuration and maintanance of a message queue
 * @author Florian Leibert
 *
 */
public class QueueParameters implements Comparable<QueueParameters>{
	

	private MessageQueue queue;
	private String queueName;
	private double weight;
	private long cpuTime;
	private transient double comparator;
	private long expectedTime;
	private int timeout;
	private int emptyCount;
	private int checkCount;
	

  private final static Log log_ =
    LogFactory.getLog(QueueParameters.class.getName());
  
	public QueueParameters() {
		this.queue = null;
		this.weight = 0;
		this.cpuTime = 0L;
		this.expectedTime = 0;
		this.queueName = null;
		this.emptyCount = 0;
		this.checkCount = 0;
	}
	
	
	/**
	 * Constructor
	 * @param queue
	 * @param name
	 * @param weight
	 * @param cpuTime
	 * @param expectedTime
	 * 
	 */
	public QueueParameters(MessageQueue queue, String name, Double weight, Long cpuTime, Long expectedTime, Integer timeout) {
		this.weight = weight;
		this.cpuTime = cpuTime;
		this.queue = queue;
		this.queueName = name;
		if (cpuTime>0 && weight>0) {
			comparator = cpuTime / weight;
		}
		this.expectedTime = expectedTime;
		this.timeout = timeout;
	}
	
	
	
	/**
	 * @return the comparator
	 */
	public double getComparator() {
		return comparator;
	}

	/**
	 * @return the cpuTime
	 */
	public synchronized long getCpuTime() {
		return cpuTime;
	}

	/**
	 * @param cpuTime the cpuTime to set
	 */
	public synchronized void setCpuTime(long cpuTime) {
		this.cpuTime = cpuTime;
		if (weight>0) {
			comparator = cpuTime / weight;
		}
	}

	/**
	 * @return the queue
	 */
	public MessageQueue getQueue() {
		return queue;
	}

	/**
	 * @param queue the queue to set
	 */
	public void setQueue(MessageQueue queue) {
		this.queue = queue;
	}

	/**
	 * @return the weight
	 */
	public double getWeight() {
		return weight;
	}

	/**
	 * @param weight the weight to set
	 */
	public void setWeight(double weight) {
		this.weight = weight;
		if (cpuTime>0) {
			comparator = cpuTime / weight;
		}
	}

	//Order is meant for the priority queue... smalles value wins
	public int compareTo(QueueParameters other) {
		//The smaller the comparator the higher the priority!
		if (this.comparator<other.comparator) {
			return -1;
		} else if (this.comparator>other.comparator) {
			return 1;
		}return 0;
	}
	


	/**
	 * @return the expectedTime
	 */
	public synchronized long getExpectedTime() {
		return expectedTime;
	}


	/**
	 * @param expectedTime the expectedTime to set
	 */
	public synchronized void setExpectedTime(long expectedTime) {
		this.expectedTime = expectedTime;
	}
	
	/**
	 * Adds the amount of cpu time to the queue.
	 * @param addCpuTime
	 */
	public synchronized void adjustCpuTime(long addCpuTime) {
		log_.debug("Adjusting CPU time");
		setCpuTime(getCpuTime() - getExpectedTime() + addCpuTime);
	}
	
	/**
	 * Adds the expected time
	 *
	 */
	public synchronized void addExTime() {
		setCpuTime(this.cpuTime + expectedTime);
	}


	/**
	 * @return the queueName
	 */
	public String getQueueName() {
		return queueName;
	}


	/**
	 * @param queueName the queueName to set
	 */
	public void setQueueName(String queueName) {
		this.queueName = queueName;
	}





	/**
	 * @return the timeout
	 */
	public int getTimeout() {
		return timeout;
	}


	/**
	 * @param timeout the timeout to set
	 */
	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}


	/**
	 * @return the emptyCount
	 */
	public int getEmptyCount() {
		return emptyCount;
	}


	/**
	 * @param emptyCount the emptyCount to set
	 */
	public void setEmptyCount(int emptyCount) {
		this.emptyCount = emptyCount;
	}
	
	/**
	 * @generated by CodeSugar http://sourceforge.net/projects/codesugar */
	
	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("[QueueParameters:");
		buffer.append(" queueName: ");
		buffer.append(queueName);
		buffer.append(" weight: ");
		buffer.append(weight);
		buffer.append(" cpuTime: ");
		buffer.append(cpuTime);
		buffer.append(" comparator: ");
		buffer.append(comparator);
		buffer.append(" expectedTime: ");
		buffer.append(expectedTime);
		buffer.append(" timeout: ");
		buffer.append(timeout);
		buffer.append("]");
		return buffer.toString();
	}


	/**
	 * @return the checkCount
	 */
	public int getCheckCount() {
		return checkCount;
	}


	/**
	 * @param checkCount the checkCount to set
	 */
	public void setCheckCount(int checkCount) {
		this.checkCount = checkCount;
	}
	

}
